/*附件相关的一些js*/
//当前浏览器是否支持socket
var end_flag = "[END]";
var wsUrl = $("#wsUrl").val();
var support = wsUrl && ('WebSocket' in window);
$(function () {

    /*检测sql按钮事件*/
    $("#check").on("click", check);
    /*处理按钮事件*/
    $("#handle").on("click", handle);
});


var websocket = null;

function buttonClickOperation() {
    $(document).on("click", "button.tail", function () {
        var dir = $("#dir").val();
        var fileName = $(this).closest("tr").find("td.d2").text();
        //判断当前浏览器是否支持WebSocket, 主要此处要更换为自己的地址
        if ('WebSocket' in window) {
            if (websocket != null) {
                websocket.close();
            }

            $("button.open").trigger("click");
            $("#detail").text(fileName);
            cleanMsg();
            console.info("ws:" + ws);
            websocket = new WebSocket(ws);
            bindEvent(dir + ";" + fileName);
        } else {
            alert('Not support websocket')
        }

    });


}


function bindEvent(message) {
    //连接发生错误的回调方法
    websocket.onerror = function () {
        setMessageInnerHTML("error");
    };

    //连接成功建立的回调方法
    websocket.onopen = function (event) {
        setMessageInnerHTML("<h3>开始处理附件</h3>");
        send(message);
    }

    //接收到消息的回调方法
    websocket.onmessage = function (event) {
        if (end_flag == event.data) {
            closeWebSocket();
            $btn.prop("disabled", false);

            return;
        }
        setMessageInnerHTML(event.data);
    }

    //连接关闭的回调方法
    websocket.onclose = function () {
        setMessageInnerHTML("<h3>closed</h3>");
    }

    //监听窗口关闭事件，当窗口关闭时，主动去关闭websocket连接，防止连接还没断开就关闭窗口，server端会抛异常。
    window.onbeforeunload = function () {
        websocket.close();
    }
}


//将消息显示在网页上
function setMessageInnerHTML(innerHTML) {
    $("#message").append(innerHTML);
    $("#filelog-container").scrollTop($("#filelog-container div").height() - $("#filelog-container").height());
}

//关闭连接
function closeWebSocket() {
    if (!websocket) {
        return;
    }
    websocket.close();
    $("#handle").prop("disabled", false);
}

//发送消息
function send(message) {
    websocket.send(message);
}

function cleanMsg() {
    $("#message").html("");
}

/* 检测sql*/
function check() {
    var $btn = $(this);
    $btn.prop("disabled", true);
    var url = htx + "/attachment/check";
    var sql = $("textarea").val();
    $.ajax(url, {data: {sql: sql}}).done(function (data) {
        if (data.code == 0) {
            $.alert({
                title: "校验成功",
                type: 'blue',
                content: data.msg
            });
        } else {
            $.alert({
                title: "出错了",
                theme: 'material',
                type: 'red',
                content: data.msg
            });
        }
    }).always(function () {
        $btn.prop("disabled", false);
    });
}

/*处理sql*/
function handle() {

    var sql = $("textarea").val();
    var target = $("#target").val();
    if (!target) {
        $.alert({
            title: "tips",
            type: 'red',
            content: '请输入目标文件夹'
        });
        return;
    }
    var $btn = $(this);
    $btn.prop("disabled", true);
    $.confirm({
        title: '确认',
        content: '确认处理吗?',
        buttons: {
            确认处理: function () {
                if (support) {
                    cleanMsg();
                    websocket = new WebSocket(wsUrl);
                    bindEvent(sql + "==" + target);
                } else {
                    _real();
                }

            },
            取消: function () {
                $btn.prop("disabled", false);
            }
        }
    })
    var url = htx + "/attachment/handle";


    function _real() {
        $.ajax(url, {data: {sql: sql, target: target}}).done(function (data) {
            if (data.code == 0) {
                $.alert({
                    title: "校验成功",
                    type: 'blue',
                    content: data.msg
                });
            } else {
                $.alert({
                    title: "出错了",
                    theme: 'material',
                    type: 'red',
                    content: data.msg
                });
            }
        }).always(function () {
            $btn.prop("disabled", false);
        });
    }

}




